<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input placeholder="请输入内容" style="width: 200px" v-model="input" clearable></el-input>
      <el-button type="warning" style="margin-left: 10px">查询</el-button>
      <el-button type="primary" style="margin-left: 10px" @click="add()">新增</el-button>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%" :default-sort="{ prop: 'date', order: 'descending' }">
        <el-table-column prop="adname" label="用户名" width="260"></el-table-column>
        <el-table-column prop="password" label="密码" width="260"></el-table-column>
        <el-table-column prop="nickname" label="昵称" width="260"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" sortable width="320"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="320"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-left: 50%;margin-top: 10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="20">
      </el-pagination>
    </div>
    <div>
      <!-- Form -->
      <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" ref="formRef">
          <el-form-item label="用户名" label-width="15%">
            <el-input v-model="form.adname" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="密码" label-width="15%">
            <el-input v-model="form.password" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="昵称" label-width="15%">
            <el-input v-model="form.nickname" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <!-- <el-form-item label="创建时间" label-width="15%">
        <el-input v-model="form.createtime" autocomplete="off" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="登录时间" label-width="15%">
        <el-input v-model="form.updatetime" autocomplete="off" style="width: 90%"></el-input>
      </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Admin",
  data() {
    return {
      input: '',
      value: false,
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      tableData: [],
      dialogFormVisible: false,
      form: {}
    }
  },
  //页面加载的时候，做一些事情，在created里面
  created() {
    this.load();
  },
  //定义一些页面上控件触发的事件调用的方法
  methods: {
    load() {
      request.get("/admin/list").then(res => {
        if (res.code === '0') {
          this.tableData = res.data;
        } else {

        }
      })
    },
    add() {
      this.form = {} //新增数据的时候清空数据
      this.dialogFormVisible = true;
    },
    edit(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
    },
    del(id) {
      request.delete(`/admin/${id}`).then(res => {
        if (res.code === '0') {
          this.tableData = this.tableData.filter(item => item.id !== id);
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          /*this.finBySearch();*/
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submit() {
      request.post("/admin/save", this.form).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    formatter(row, column) {
      return row.address;
    }
  },
}
</script>
